<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\common\logic;
use app\admin\model\Attachment as AttachmentModel;


/**
 * oss相关逻辑
 * @package app\common\logic
 */
class Attachmentlogic {

    /**
     * 删除附件
     * @param string $ids 附件id
     * @author jason wang <120907037@qq.com>
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($ids = '')
    {
        //$ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        
        if (empty($ids)) $this->error('缺少主键');

        $files_path = AttachmentModel::where('id', 'in', $ids)->column('path,thumb', 'id');

        foreach ($files_path as $value) {
            $real_path       = realpath(config('upload_path').'/../'.$value['path']);
            $real_path_thumb = realpath(config('upload_path').'/../'.$value['thumb']);

            if (is_file($real_path) && !unlink($real_path)) {
                return array_out(-1001, '删除失败');
            }
            if (is_file($real_path_thumb) && !unlink($real_path_thumb)) {
                return array_out(-1002, '删除缩略图失败');
            }
        }
        if (AttachmentModel::where('id', 'in', $ids)->delete()) {
            // 记录行为
            $ids = is_array($ids) ? implode(',', $ids) : $ids;
            action_log('attachment_delete', 'admin_attachment', 0, UID, $ids);
            return array_out(200, '删除成功');
        } else {
            return array_out(-1003, '删除失败');
        }
    }


}